<?php
namespace app\admin\controller;


use app\common\model\BusInquiry;
use app\common\model\SuppliersCompany;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Controller;

/**
 * 企业管理
 * Class Realdata
 * @package app\gygt\controller
 */
class Test extends Admin
{
    public function upInquirySupplier()
    {
        $data['sale_price_state'] = ['>',0];
        $model = new BusInquiry();
        $lst = $model->field('id,sale_price_state,from_quotation_selection_json')->where($data)->whereNull('sure_company_name')->select();
        foreach ($lst as $item){
            $up_data = [
                'quote_company_name'=>$item['from_quotation_selection_json']['add_company_name'],
                'quote_company_id'=>$item['from_quotation_selection_json']['add_company_id'],
            ];
            if($item['sale_price_state'] == 3){
                $up_data['sure_company_name'] = $up_data['quote_company_name'];
                $up_data['sure_company_id'] = $up_data['quote_company_id'];
            }
            $model->where(['id'=>$item['id']])->update($up_data);
        }
        json_success_msg(count($lst));
    }
    public function testadd()
    {
        $data = input();
        $spreadsheet = IOFactory::load('./template/personal/quotation.xlsx');
        $inquiryModel = new BusInquiry();
        $supplierCompanyModel = new SuppliersCompany();
        $supplierCompanyInfo = $supplierCompanyModel->where(['id'=>$data['add_company_id']])->field('suppliers_company_name,address_area,address_detail,contact_person_phone,file_template,tax_rate')->find();
        $info = $inquiryModel->with(['detail_quota'])->where(['id'=>$data['id']])->find();
        // 获取当前活动的工作表(默认是第一个工作表)
        $detail_quota = $info['detail_quota'];
        $eq_count = count($detail_quota);

        $worksheet = $spreadsheet->getActiveSheet();
        $worksheet->getRowDimension(2)->setRowHeight(35);
        for ($i=3;$i<=24;$i++){
            $worksheet->getRowDimension($i)->setRowHeight(25);
        }
        // B2
        $worksheet->setCellValue('B2',$supplierCompanyInfo['suppliers_company_name'].'报价单');
        $worksheet->setCellValue('C3',$supplierCompanyInfo['address_area'].$supplierCompanyInfo['address_detail']);
        $worksheet->setCellValue('I3',$supplierCompanyInfo['contact_person_phone']);
        $worksheet->setCellValue('L3',date('Y.m.d'));
        //第4行
        $worksheet->setCellValue('C4',$info['bus_from_company_name']);
        $worksheet->setCellValue('F4',$info['vehicle_license_num']?:'');
        $worksheet->setCellValue('I4',$info['car_series']?:'');
        $worksheet->setCellValue('L4',$info['car_model']?:'');
        //第5行
        $worksheet->getCell('C5')->setValue($info['loss_assessor']?:'');
        $worksheet->getCell('F5')->setValue($info['vehicle_license_num']?:'');
        $worksheet->getCell('I5')->setValue($info['bus_user_name']?:'');
        $worksheet->getCell('L5')->setValue($info['bus_user_phone']?:'');
        //第6行
        $worksheet->getCell('C6')->setValue($info['repair_company_name']?:'');
        $worksheet->getCell('F6')->setValue($info['repair_company_phone']?:'');
        //第7行
        $remark = '该报价含运费含'.$supplierCompanyInfo['tax_rate'].'%专票';
        $remark = $info['common']?$info['common'].'，'.$remark:$remark;
        $worksheet->getCell('C7')->setValue($remark);

        $startNum = 15;
        $startRow = 23;
        $numberOfRowsToInsert = $eq_count>15? $eq_count- 15:0;
        if($eq_count>15){
            // 插入行
            for ($i = $numberOfRowsToInsert; $i >= 1; $i--) {
                $worksheet->insertNewRowBefore($startRow + 1, 1);
            }
            // 填充数据
            for ($row = $startRow; $row <= $startRow + $numberOfRowsToInsert; $row++) {
                $worksheet->setCellValue('A' . $row, $startNum+($row - $startRow));
                $worksheet->getRowDimension($row)->setRowHeight(25);
            }
            $worksheet->setCellValue('E' . ($startRow+$numberOfRowsToInsert+1), "=sum(E9:E". ($startRow+$numberOfRowsToInsert).')');
            $worksheet->setCellValue('F' . ($startRow+$numberOfRowsToInsert+1), "=sum(F9:F". ($startRow+$numberOfRowsToInsert).')');
            $worksheet->setCellValue('G' . ($startRow+$numberOfRowsToInsert+1), "=sum(G9:G". ($startRow+$numberOfRowsToInsert).')');
            $worksheet->setCellValue('H' . ($startRow+$numberOfRowsToInsert+1), "=sum(H9:G". ($startRow+$numberOfRowsToInsert).')');
            $worksheet->setCellValue('I' . ($startRow+$numberOfRowsToInsert+1), "=sum(I9:G". ($startRow+$numberOfRowsToInsert).')');
            $worksheet->setCellValue('J' . ($startRow+$numberOfRowsToInsert+1), "=sum(J9:G". ($startRow+$numberOfRowsToInsert).')');
        }

        for ($i = 0;$i < $eq_count;$i++) {
            $row = $i+9;
            $service_price = in_array('service',$detail_quota[$i]['quote_quilty'])?round($detail_quota[$i]['qulity_service_price']*(100+$supplierCompanyInfo['tax_rate'])/100*$detail_quota[$i]['num']):'';
            $org_price = in_array('org',$detail_quota[$i]['quote_quilty'])?round($detail_quota[$i]['qulity_org_price']*(100+$supplierCompanyInfo['tax_rate'])/100*$detail_quota[$i]['num']):'';
            $matching_price = in_array('matching',$detail_quota[$i]['quote_quilty'])?round($detail_quota[$i]['qulity_matching_price']*(100+$supplierCompanyInfo['tax_rate'])/100*$detail_quota[$i]['num']):'';
            $complete_price = in_array('complete',$detail_quota[$i]['quote_quilty'])?round($detail_quota[$i]['qulity_complete_price']*(100+$supplierCompanyInfo['tax_rate'])/100*$detail_quota[$i]['num']):'';
            $suitable_price = in_array('suitable',$detail_quota[$i]['quote_quilty'])?round($detail_quota[$i]['qulity_suitable_price']*(100+$supplierCompanyInfo['tax_rate'])/100*$detail_quota[$i]['num']):'';
            $eq_name = $detail_quota[$i]['eq_name'];
            if($detail_quota[$i]['oe_no']){
                $eq_name = $detail_quota[$i]['eq_name'] . '(OE码：'.$detail_quota[$i]['oe_no'].')';
            }
            $worksheet->setCellValue('C' . $row, $eq_name);
            $worksheet->setCellValue('E' . $row, $detail_quota[$i]['num']);
            $worksheet->setCellValue('F' . $row, $service_price);
            $worksheet->setCellValue('G' . $row, $org_price);
            $worksheet->setCellValue('H' . $row, $matching_price);
            $worksheet->setCellValue('I' . $row, $complete_price);
            $worksheet->setCellValue('J' . $row, $suitable_price);
            $worksheet->setCellValue('K' . $row, $detail_quota[$i]['common']);
        }
        $worksheet->setCellValue('K' . (28+$numberOfRowsToInsert), $supplierCompanyInfo['suppliers_company_name']);
        // 保存Excel文件
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $files = $info['vehicle_license_num'].'报价单'.date('YmdHis').'.xlsx';
//        $writer->save('./static/fileds/'.$files);
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename='.$files);
        header('Cache-Control: max-age=0');
        $writer->save('php://output');
        exit;
        json_success_msg('https://desheng.hot68.cn/htqipei/htapi/public/static/fileds/'.$files);

        /*for ($i = 2;$i <= count($info) + 1;$i++) {
            for($j = 0;$j < count($table_value);)
                foreach ($table_value as $v) {
                    $objPHPExcel->getActiveSheet()->setCellValue("$cellName[$j]$i", $info[$i - 2][$v]);
                    $j++;
                }
        }
        header('Content-Type: applicationnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$table_name.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;*/
    }

}
